package fb.chatter.controller;

import java.net.URL;
import java.util.ResourceBundle;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import fb.chatter.constant.Constant;
import fb.chatter.model.User;
import fb.chatter.utils.ExceptionUtils;

public class ChattingFormController implements Initializable {

	@FXML
	private AnchorPane rootPane;
	@FXML
	private ImageView imvMyPhoto;
	@FXML 
	private ImageView imvFriendPhoto;
	@FXML
	private Button btnSearch;
	@FXML
	private TextArea txaTypingMessage;
	@FXML
	private TextArea txaMessages;
	
	/**
	 * The chatting friend
	 */
	private User chattingFriend;
	
	public ChattingFormController(User friend){
		
		this.chattingFriend = friend;
	}
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {

		this.imvFriendPhoto.setImage(this.chattingFriend.getPicture());
		this.imvMyPhoto.setImage(Constant.currentUser.getPicture());
		
		Stage stage = new Stage();
		stage.setTitle(this.chattingFriend.getName());
		stage.getIcons().add(new Image("/logo-chatter.png"));
		stage.setScene(new Scene(this.rootPane, 500, 400));
		stage.setResizable(false);
		stage.show();
		
	}

	/**
	 * 
	 */
	@FXML
	private void btnSendClick(MouseEvent event){
		
		System.out.println(this.chattingFriend.getUsername() + "@facebook.com");
		String msg = this.txaTypingMessage.getText();
		
		EmailSender emailSender = new EmailSender(this.chattingFriend.getUsername() + "@facebook.com", msg);
		Thread emailSenderThread = new Thread(emailSender);
		emailSenderThread.start();
		
		updateUIAfterSending();
	}
	
	/**
	 * To update UI after sending an email
	 */
	private void updateUIAfterSending() {
		
		String sentMsg = txaTypingMessage.getText();
		txaTypingMessage.setText("");
		txaMessages.appendText("\n" + Constant.currentUser.getName() + ": " + sentMsg);
	}


	/**
	 * This class is used for sending an email to a facebook friend
	 * @author Horizon
	 *
	 */
	private class EmailSender extends Task<Integer>{

		/**
		 * Destination email address
		 */
		private String desEmail;
		
		/**
		 * The content that will be sent
		 */
		private String content;
		
		/**
		 * @param desEmail
		 * @param content
		 */
		public EmailSender(String desEmail, String content) {
			
			this.desEmail = desEmail;
			this.content = content;
		}

		@Override
		protected Integer call() throws Exception {
			
			sendEmail(desEmail, content);
			return null;
		}
		
		/**
		 * To send the given content string to the given destination email address
		 * 
		 * @param desEmail
		 * @param content
		 */
		private void sendEmail(String desEmail, String content){
			
			try {
				Email email = new SimpleEmail();
				email.setHostName(Constant.SMTP_GMAIL_SERVER);
				email.setAuthenticator(new DefaultAuthenticator(Constant.CURRENT_USER_NAME, Constant.CURRENT_PASSWORD));
				email.setSSLOnConnect(true);
				email.setFrom(Constant.CURRENT_USER_NAME + "@gmail.com");
				email.setSubject("TestMail");
				email.setMsg(content);
//				email.addTo(desEmail);
				email.addTo("nguyentronghoan@facebook.com");
				email.send();
			} catch (EmailException e) {
				ExceptionUtils.log(e);
			}
		}
	}
}
